package com.gf.common.lucene.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.gf.base.type.IndexType;
import com.gf.common.constant.Constants;
import com.gf.common.lucene.service.LuceneService;
import com.gf.common.lucene.service.LuceneSynchroService;
import com.gf.common.util.WebHelper;
import com.gf.exam.dao.ExamDao;
import com.gf.exam.model.Exam;
import com.gf.user.dao.UserBaseDao;
import com.gf.user.model.User;

@Service("luceneSynchroService")
public class LuceneSynchroServiceImpl implements LuceneSynchroService {

	@Value("${lucene.page.pageSize}")
	private int lucenePageSize;

	@Autowired
	private ExamDao examDao;

	@Autowired
	private UserBaseDao userBaseDao;

	@Autowired
	private LuceneService luceneService;

	// -1不用更新 0用户更新 1测试项目更新 2所有都更新
	private int mark;

	/**
	 * -1不用更新 0用户更新 1测试项目更新 2所有都更新
	 */
	@Override
	public void checkTimestamp() {
		boolean userUpdate = false;
		boolean examUpdate = false;

		Exam exam = examDao.queryLatestExam();
		User user = userBaseDao.queryLatestUser();

		long luceneLatest = WebHelper
				.getSessionAttribute(Constants.SESSION_LUCENE_TIMESTAMP);

		if (user != null) {
			long userLatest = user.getCreatetime().getTime();
			if (userLatest <= luceneLatest) {
				userUpdate = false;
			}
		}

		if (exam != null) {
			long examLatest = exam.getCreatetime().getTime();
			if (examLatest > luceneLatest) {
				examUpdate = true;
			}
		}

		if (userUpdate && examUpdate) {
			mark = 2;
		} else if (!userUpdate && examUpdate) {
			mark = 1;
		} else if (userUpdate && !examUpdate) {
			mark = 0;
		} else {
			mark = -1;
		}
	}

	@Override
	public void synchro() {
		// 两者都更新
		if (mark == 2) {
			synchroExam();
			synchroUser();
		}
		// 更新exam
		else if (mark == 1) {
			synchroExam();
		}
		// 更新 user
		else if (mark == 0) {
			synchroUser();
		}

	}

	@Override
	public void rebuild() {
		// 用户
		int userNumber = userBaseDao.queryAllUserNumber();
		int totalPages = userNumber / lucenePageSize + 1;
		for (int i = 1; i <= totalPages; i++) {
			List<User> users = userBaseDao.queryAllUsers(i, lucenePageSize);
			luceneService.add(IndexType.USER, users);
		}

		// exam
		int eventNumber = examDao.queryExamNumber();
		totalPages = eventNumber / lucenePageSize + 1;
		for (int i = 1; i <= totalPages; i++) {
			List<Exam> exams = examDao.queryAllExamForLucene(i);
			luceneService.add(IndexType.EXAM, exams);
		}

	}

	@Override
	public void updateTimstamp() {
		long now = System.currentTimeMillis();
		WebHelper.setSessionAttribute(Constants.SESSION_LUCENE_TIMESTAMP, now);
	}

	/**
	 * 更新exam索引
	 */
	private void synchroExam() {
		Date date = new Date(
				(Long) WebHelper
						.getSessionAttribute(Constants.SESSION_LUCENE_TIMESTAMP));
		List<Exam> exams = examDao.queryExamByUpdatetime(date);

		luceneService.add(IndexType.EXAM, exams);
	}

	/**
	 * 更新user索引
	 */
	private void synchroUser() {
		Date date = new Date((Long) WebHelper.getSessionAttribute(Constants.SESSION_LUCENE_TIMESTAMP));

		List<User> users = userBaseDao.queryUserByCreatetime(date);

		luceneService.add(IndexType.USER, users);
	}

}
